﻿using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Web;
using System.Xml.Serialization;
using System.Collections.Generic;

namespace QD.Web.Upload
{
  

    public class UploadFileConfig
    {
        [XmlAttribute("Key")]
        public String Key { get; set; }

        [XmlAttribute(AttributeName = "AllowUpload")]
        public Boolean AllowUpload { get; set; }

        [XmlAttribute(AttributeName = "Prefix")]
        public String Prefix { get; set; }
        [XmlAttribute(AttributeName = "Suffix")]
        public String Suffix { get; set; }

        String _allowExt = null;
        [XmlAttribute(AttributeName = "AllowExt")]
        public String AllowExt { get { return _allowExt; } set { _allowExt = value.ToLower(); } }

        String _forbitExt = null;
        [XmlAttribute(AttributeName = "ForbitExt")]
        public String ForbitExt { get { return _forbitExt; } set { _forbitExt = value.ToLower(); } }

        [XmlAttribute(AttributeName = "RootPath")]
        public String RootPath { get; set; }


        [XmlAttribute(AttributeName = "PathStyle")]
        public UploadNameStyle PathStyle { get; set; }

        [XmlAttribute(AttributeName = "MaxFileSize")]
        public Int32 MaxFileSize { get; set; }

        public String NameStyleDirectory
        {
            get
            {
                String directory = null;

                DateTime now = DateTime.Now;
                if (PathStyle == UploadNameStyle.With_Year_Month)
                    directory = String.Concat(now.ToString("yyyy"), Path.DirectorySeparatorChar, now.ToString("MM"));
                else if (PathStyle == UploadNameStyle.With_Year_Month_Day)
                    directory = String.Concat(now.ToString("yyyy"), Path.DirectorySeparatorChar, now.ToString("MM"), Path.DirectorySeparatorChar, now.ToString("dd"));

                return directory;
            }
        }
        public String FullPath
        {
            get
            {
                return Path.Combine(RootPath, NameStyleDirectory);
            }
        }

        public String GenerateName(String filename, Boolean random = true)
        {
            String ext = Path.GetExtension(filename);
            if (random)
                filename = String.Concat(DateTime.Now.Ticks, ext);
            Int32 dotIdx = filename.LastIndexOf('.');
            String name = dotIdx > 0 ? filename.Substring(0, dotIdx) : filename;
            return String.Concat(this.Prefix, name, this.Suffix, ext);
        }

        public virtual void SaveFile(HttpPostedFile file, String saveFileName)
        {
            String saveFullPath = Path.Combine(this.FullPath, saveFileName);
            file.SaveAs(saveFullPath);
        }

    }



}
